package com.alinesno.cloud.common.web.base.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.web.base.utils.WebUploadUtils;

import cn.hutool.core.io.IORuntimeException;

/**
 * 下载路径
 * 
 * @author WeiXiaoJin
 * @sine 2019年3月14日 下午8:55:21
 */
@Controller
@RequestMapping("/web/storage/")
public class WebStorageController extends BaseController {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	// @Value("${excel.temp.dir}")
	private String stackFileExport; // 输出路径

	@Autowired
	private WebUploadUtils webUploadUtils;

	/**
	 * 文件上传
	 * 
	 * @param fileLoalAbcPath
	 * @param strategy
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@PostMapping("/upload")
	public ResponseBean uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		String message = webUploadUtils.storageFile(file, fileName);
		return ResponseGenerator.genFailResult(message);
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("/download")
	public void downloadFile(HttpServletRequest request, String fileName, HttpServletResponse response) {

		log.debug("file name = {}", fileName);

		if (fileName != null) {
			File file = new File(stackFileExport + "/" + fileName);

			if (file.exists()) {
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
				byte[] buffer = new byte[1024];
				FileInputStream fis = null;
				BufferedInputStream bis = null;

				try {
					fis = new FileInputStream(file);
					bis = new BufferedInputStream(fis);
					OutputStream os = response.getOutputStream();
					int i = bis.read(buffer);
					while (i != -1) {
						os.write(buffer, 0, i);
						i = bis.read(buffer);
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (bis != null) {
						try {
							bis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (fis != null) {
						try {
							fis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

	/**
	 * 单文件上传
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@PostMapping("/singleUpload")
	@ResponseBody
	public ResponseBean upload(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException {

		if (file.isEmpty()) {
			return fail();
		}

		String fileName = file.getOriginalFilename();
		File dest = new File(localPath + fileName);

		file.transferTo(dest);
		log.debug("上传成功");
		return ok();
	}

	/**
	 * 多个文件上传
	 * 
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IORuntimeException 
	 */
	@PostMapping("/multiUpload")
	@ResponseBody
	public ResponseBean multiUpload(HttpServletRequest request) throws IORuntimeException, IOException {
		List<Map<String, Object>> map = multiUploadFile(request , false);
		return ok(map.get(0).get("saveFilename")+"") ; 
	}

	/**
	 * BootstrapInput多文件上传
	 * @param request
	 * @return
	 * @throws IORuntimeException
	 * @throws IOException
	 */
	@PostMapping("/bootstrapInputMultiUpload")
	@ResponseBody
	public List<Map<String, Object>> bootstrapInputMultiUpload(HttpServletRequest request) throws IORuntimeException, IOException {
		List<Map<String, Object>> map = multiUploadFile(request , false);
		return map ; 
	}
	
	/**
	 * 云存储文件上传
	 * @param request
	 * @return
	 * @throws IORuntimeException
	 * @throws IOException
	 */
	@PostMapping("/bootstrapInputCloudUpload")
	@ResponseBody
	public List<Map<String, Object>> bootstrapInputCloudUpload(HttpServletRequest request) throws IORuntimeException, IOException {
		List<Map<String, Object>> map = multiUploadFile(request , true);
		return map ; 
	}

	/**
	 * 默认登陆上传界面 
	 * @return
	 */
	@GetMapping("/pages/default")
	public String uploadDefaultPage() {
		return "common/upload/default" ; 
	}

}
